<template>
  <a-modal :title="title" :destroyOnClose="true" :width="width" :visible="visible" @ok="handleOk" :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }" @cancel="handleCancel" cancelText="关闭">
    <p class="p1">评论信息：{{tableItemData.commentContent}}</p>
    <p class="p2">点赞数：{{tableItemData.likeCount}}</p>
    <!--定义表格-->
    <BasicTable @register="registerTable">
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
    <CommentModal
      ref="commentModal"
      @success="handleSuccess"
      :tableId="tableItemData.id"
    ></CommentModal>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineExpose } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import BookCommentForm from './BookCommentForm.vue'
  import CommentModal from './CommentModal.vue'
  import { defHttp } from '/@/utils/http/axios';
  import { useListPage } from '/@/hooks/system/useListPage';
  import {
    deleteLittleOne,
    getExportUrl,
    getImportUrl,
    list
  } from "@/views/novel/bookComment/BookComment.api";
  import {commentColumns} from "@/views/novel/bookComment/BookComment.data";

  const title = ref<string>('');
  const width = ref<number>(1200);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const registerForm = ref();
  const emit = defineEmits(['register', 'success']);
  const dataSource=ref()

  const tableItemData=ref(null)
  const commentModal = ref();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '',
      api: demoListApi,
      columns:commentColumns,
      dataSource,
      canResize:false,
      useSearchForm: false,
      showIndexColumn: true,
      showTableSetting: false,
      tableSetting:{
        redo: false,
        // 是否显示尺寸调整按钮
        size: false,
        // 是否显示字段调整按钮
        setting: false,
        // 是否显示全屏按钮
        fullScreen: false
      },
      actionColumn: {
        width: 180,
        fixed: 'right',
      },
    },
  });

  const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] = tableContext;

  async function getData(params) {
    const data=await defHttp.get({ url: '/novel/bookComment/detail/list', params });
    console.log(data)
    dataSource.value=data.records
  }


  /**
   *
   * 新增
   */
  function add() {
    title.value = '新增';
    visible.value = true;
    nextTick(() => {
      registerForm.value.add();
    });
  }


  function demoListApi(params){
    let obj={
      commentId:tableItemData.value.id,
      pageNo:params.pageNo,
      pageSize:params.pageSize,
    }
    return defHttp.get({ url: '/novel/bookComment/detail/list', params:obj });
  }

  /**
   * 编辑
   * @param record
   */
  async function edit(record) {
    reload()
    title.value =  '详情'
    visible.value = true;
    tableItemData.value=record
  }

  /**
   * 确定按钮点击事件
   */
  function handleOk() {
    registerForm.value.submitForm();
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    handleCancel();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '回复',
        onClick: handleDetail.bind(null, record),
      }, {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        }
      }
    ];
  }

  /**
   * 回复
   */
  function handleDetail(record: Recordable) {
    console.log(record,'zzxxcc')
    commentModal.value.disableSubmit = false;
    commentModal.value.edit(record,1);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteLittleOne({ id: record.id }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  defineExpose({
    add,
    edit,
    disableSubmit,
  });
</script>

<style lang="less">
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
  /deep/.ant-table-title{
    display: none;
  }
  .p1{
    margin-left: 10px;
    margin-top: 10px;
    margin-bottom: 0;
  }
  .p2{
    margin-left: 10px;
    margin-bottom: 0;
  }
</style>
